// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var router = require("../hash/pageRouter");
	var dialog = require("dialog");
	var validate = require("validate");
	var databind = require("databind");
	var css_detail_info = require("./financeEarning.css");
	var constant = require("constant");
	var enterpriseId;
	var pageIndex = 1,
		pageSize = 10,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var init = function(page) {
		enterpriseId = page.params.enterpriseId;
		inquireFinanceCommissionInfoPageList();
		initEvent();
	};
	var initEvent = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			top.location.href = "../login/login.html";
		}
		
		$("#financeEarning .extract").unbind("click").click(function(){
			if ($("#financeEarning input[type=checkbox]:checked").length <= 0) {
				base.alert(2, "请选择需要提取佣金的融资!");
				return;
			}
			var financeIds = [];
			$.each($("#financeEarning input[type=checkbox]:checked"), function() {
				var f = $(this).parents("tr").attr("financeId");
				financeIds.push(f);
			});
			createFinanceMoneyStatementEarning(financeIds);
		});
	};
	
	//企业查询融资信息列表
    var createFinanceMoneyStatementEarning = function(financeIds){
		base.fnAjax({
			action: "createFinanceMoneyStatementEarning.json",
			async: false,
			oData: {
           		financeIds: financeIds
            }, //传递参数
            fnSuccess: function(data) {
                if (data.status == 200) {
                	base.alert(1, "生成成功！");
                	router.route("client/financeEarning.html");
                } else {
                    base.alert(2, data.message);
                }
            }
		});
	};

	//收益列表
	var inquireFinanceCommissionInfoPageList = function() {
		base.fnAjax({
			action: "inquireFinanceCommissionInfoPageList.json",
			oData: {
				pageIndex: pageIndex,
				pageSize: pageSize,
				enterpriseId: enterpriseId
			}, //传递参数
			fnSuccess: function(data) { //成功回调
				if (data.status == 200) {
					juicer.register("getValueByKey", constant.getValueByKey);
					var tpl = $("#memberGradeTpl").html();
					var html = juicer(tpl, data);
					$("#memberGradeTable").html(html);
					
					$("#financeEarning .detail").unbind("click").click(function(){
                		router.route("financing/commissionDetail.html?financeId="+$(this).parents("tr").attr("financeId") + "&expenseItem=佣金");
                	});
					
					totalPageCount = data.totalPageCount;
                    totalRecordCount = data.totalRecordCount;
                    base.pageControl(
                    	$("#memberGradeaudit .pagerCon"),
                        totalRecordCount,
                        data.pageIndex,
                        data.pageSize,
                        function(p) {
                            pageIndex = p;
                            inquireFinanceCommissionInfoPageList();
                    });
				} else {
					base.alert(3, data.message);
				}
			}
		});
	}
	
	return {
		init: init
	};
});